package ar.com.larreta.reportes;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import ar.com.larreta.procesos.ArchivoDescargable;


public class UtilidadesReportes {
	
	public static final String PDF = ".pdf";
	public static final String XLS = ".xls";
	public static final String TXT = ".txt";
	public static final String POI_XLS = "poi.xls";
	
	public static final String CONTENT_DISPOSITION = "Content-Disposition";
	public static final String ATTACHMENT_FILENAME = "attachment; filename=";
	public static final String REG_EXP_SEPARATOR = ";";

	/** The Constant log. */
	private static final Logger log = Logger.getLogger(UtilidadesReportes.class);
	
	private Map reportsTypes;
	
	private String name;
	
	
	private String columnNames;
	private String columnDescritions;
	private Collection colColumnNames;
	private Collection colColumnDescritions;
	
	public Collection getColColumnNames() {
		return colColumnNames;
	}

	public Collection getColColumnDescritions() {
		return colColumnDescritions;
	}

	public UtilidadesReportes(String templatePath){
		reportsTypes = new HashMap();
		setReportsTypes(templatePath);
	}
    
	public ArchivoDescargable obtenerReporte(String nombre, String reportType, Collection dataSource){
		
		TipoReporte reportUtilType = (TipoReporte) reportsTypes.get(reportType);
		
		if (reportUtilType==null){
			//FIXME: Lanzar excepcion;
		}
		ArchivoDescargable generado = null;
		try {
			ByteArrayOutputStream outputStream = reportUtilType.getOutputStream(dataSource);
			generado = new ArchivoDescargable(nombre + reportType, reportUtilType.getContentType(), outputStream);
		} catch (IOException e) {
			log.error("Ocurrio un error generando el reporte", e);
		}
		return generado;
	}
	
    /**
	 * Define los tipos de reporte que soporta
	 * @param path
	 */
	private void setReportsTypes(String path) {
		reportsTypes.put(PDF, new PDF(this, path));
		reportsTypes.put(XLS, new XLS(this, path));
		reportsTypes.put(TXT, new TXT(this, path));
		reportsTypes.put(POI_XLS, new PoiXLS(this, path));
	}
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
	
    public String getColumnNames() {
		return columnNames;
	}

	public void setColumnNames(String columnNames) {
		this.columnNames = columnNames;
		if ("".equals(columnNames)){
			colColumnNames = Arrays.asList(columnNames.split(REG_EXP_SEPARATOR));
		}
	}

	public String getColumnDescritions() {
		return columnDescritions;
	}

	public void setColumnDescritions(String columnDescritions) {
		this.columnDescritions = columnDescritions;
		if ("".equals(columnDescritions)){
			colColumnDescritions = Arrays.asList(columnDescritions.split(REG_EXP_SEPARATOR));
		}
	}
}
